package belf.migrate.engine.job.verify;

import belf.migrate.core.number.NumberFormatUtil;
import belf.migrate.api.job.JobCode;
import belf.migrate.engine.job.model.verify.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VerifyAllTool {

    private VerifyTableTool verifyTableTool;

    private VerifyViewTool verifyViewTool;

    private VerifyTriggerTool verifyTriggerTool;

    private VerifyFuncTool verifyFuncTool;

    private VerifySeqTool verifySeqTool;

    private VerifySynonymTool verifySynonymTool;

//    private VerifyIndexTool verifyIndexTool;

    private VerifyTypeTool verifyTypeTool;

    public VerifyAllTool(
            VerifyTableTool verifyTableTool,
            VerifyViewTool verifyViewTool,
            VerifyTriggerTool verifyTriggerTool,
//            VerifyIndexTool verifyIndexTool,
            VerifySeqTool verifySeqTool,
            VerifySynonymTool verifySynonymTool,
            VerifyFuncTool verifyFuncTool,
            VerifyTypeTool verifyTypeTool) {
        this.verifyTableTool = verifyTableTool;
        this.verifyViewTool = verifyViewTool;
        this.verifyTriggerTool = verifyTriggerTool;
//        this.verifyIndexTool = verifyIndexTool;
        this.verifySeqTool = verifySeqTool;
        this.verifySynonymTool = verifySynonymTool;
        this.verifyFuncTool = verifyFuncTool;
        this.verifyTypeTool = verifyTypeTool;
    }

    public VerifyAllResult run() {
        VerifyAllResult result = new VerifyAllResult();
        VerifyTableResult tableResult = verifyTableTool.run();
        VerifyViewResult viewResult = verifyViewTool.run();
        VerifyTriggerResult triggerResult = verifyTriggerTool.run();
//        VerifyIndexResult indexResult = verifyIndexTool.run();
        VerifySeqResult seqResult = verifySeqTool.run();
        VerifySynonymResult synonymResult = verifySynonymTool.run();
        VerifyFunctionResult funcResult = verifyFuncTool.run();
        VerifyTypeResult typeResult = verifyTypeTool.run();
        int allSize = 0;
        int successCount = 0;
        int failCount = 0;
        int code = JobCode.SUCCESS;
        if (tableResult != null) {
            allSize += tableResult.getAllSize();
            successCount += tableResult.getSuccessCount();
            failCount += tableResult.getFailCount();
            if (JobCode.SUCCESS != tableResult.getCode()) {
                code = tableResult.getCode();
            }
        }
        if (viewResult != null) {
            allSize += viewResult.getAllSize();
            successCount += viewResult.getSuccessCount();
            failCount += viewResult.getFailCount();
            if (JobCode.SUCCESS != viewResult.getCode()) {
                code = viewResult.getCode();
            }
        }
        if (triggerResult != null) {
            allSize += triggerResult.getAllSize();
            successCount += triggerResult.getSuccessCount();
            failCount += triggerResult.getFailCount();
            if (JobCode.SUCCESS != triggerResult.getCode()) {
                code = triggerResult.getCode();
            }
        }
//        if (indexResult != null) {
//            allSize += indexResult.getAllSize();
//            successCount += indexResult.getSuccessCount();
//            failCount += indexResult.getFailCount();
//            if (JobCode.SUCCESS != indexResult.getCode()) {
//                code = indexResult.getCode();
//            }
//        }
        if (seqResult != null) {
            allSize += seqResult.getAllSize();
            successCount += seqResult.getSuccessCount();
            failCount += seqResult.getFailCount();
            if (JobCode.SUCCESS != seqResult.getCode()) {
                code = seqResult.getCode();
            }
        }
        if (synonymResult != null) {
            allSize += synonymResult.getAllSize();
            successCount += synonymResult.getSuccessCount();
            failCount += synonymResult.getFailCount();
            if (JobCode.SUCCESS != synonymResult.getCode()) {
                code = synonymResult.getCode();
            }
        }
        if (funcResult != null) {
            allSize += funcResult.getAllSize();
            successCount += funcResult.getSuccessCount();
            failCount += funcResult.getFailCount();
            if (JobCode.SUCCESS != funcResult.getCode()) {
                code = funcResult.getCode();
            }
        }
        if (typeResult != null) {
            allSize += typeResult.getAllSize();
            successCount += typeResult.getSuccessCount();
            failCount += typeResult.getFailCount();
            if (JobCode.SUCCESS != typeResult.getCode()) {
                code = typeResult.getCode();
            }
        }
        result.setAllSize(allSize);
        result.setSuccessCount(successCount);
        result.setFailCount(failCount);
        float successPercent = allSize == 0 ? 1 : (float)successCount / allSize;
        float failPercent = allSize == 0 ? 0 : (float)failCount / allSize;
        result.setSuccessPercent(NumberFormatUtil.numberToString(successPercent, "0.0%"));
        result.setFailPercent(NumberFormatUtil.numberToString(failPercent, "0.0%"));
        result.setTableResult(tableResult);
        result.setViewResult(viewResult);
        result.setTriggerResult(triggerResult);
//        result.setIndexResult(indexResult);
        result.setSeqResult(seqResult);
        result.setSynonymResult(synonymResult);
        result.setFuncResult(funcResult);
        result.setTypeResult(typeResult);
        result.setCode(code);
        return result;
    }

}
